(function(factory) {
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define(factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	/***
	 * 桌面右键菜单
	 * @class HteOS.metro.ContextMenu
	 * */
	HteOS.metro.ContextMenu = {
		
		
		containerContextMenu : [{
					id :'fullscreen',
					icon : 'glyphicon glyphicon-fullscreen',
					name :'全屏显示',
					handler :function(){
						var el = document.body;
						var rfs = el.requestFullScreen
								|| el.webkitRequestFullScreen
								|| el.mozRequestFullScreen
								|| el.msRequestFullScreen;
						if (typeof rfs != "undefined" && rfs) {
							rfs.call(el);
						} else if (typeof window.ActiveXObject != "undefined") {
							// for Internet Explorer
							try {
								var wscript = new ActiveXObject("WScript.Shell");
								if (wscript != null) {
									wscript.SendKeys("{F11}");
								}
							} catch (e) {
								HteOS.Notification.show("", "应用提示",
										"无法进行全屏显示，请点击F11键进行全屏显示", true);
							}
						}
					}
				},{
					id :'locker',
					icon : 'glyphicon glyphicon-lock',
					name :'锁定屏幕',
					handler :function(){
						if(HteOS.User.logon === true){
							HteOS.Locker.lock();
						}
					}
				},/*{
					html : "<a role=\"menuitem\" href=\"https://chrome.google.com/webstore/detail/hteos-web%E6%A1%8C%E9%9D%A2%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91%E6%A1%86%E6%9E%B6/bjbnjaagmemfadhiangcankhejdfjcob/details?utm_source=chrome-ntp-icon\" target=\"_blank\">" + 
								"<img src=\""+HteOS.basePath+"images/chrome_logo.png\" height=\"19\" width=\"19\" style=\"margin-top: -3px;margin-left: -3px;\">&nbsp;安装到Chrome" + 
							"</a>"
				},*/'-',{
					id :'manager',
					icon : 'glyphicon glyphicon-list',
					name :'应用管理',
					handler :function(){
						HteOS.TaskManager.start({
								id : 'appmanager',
								name : "应用管理",
								path : HteOS.basePath+"tpl/overview.html",
								mode : 'dock'
							});
					}
				},{
					id :'store',
					icon : 'glyphicon glyphicon-folder-close',
					name :'应用市场',
					handler :function(){
						var app = HteOS.AppManager.getByKey("store");
						if (app) {
							HteOS.TaskManager.start(app);
						} else {
							HteOS.Notification.show("", "应用提示",
									"运行[应用市场]失败，请先安装[应用市场]应用");
						}
					}
				},'-',{
					id: 'wallpaper',
					icon: 'glyphicon glyphicon-cog',
					name: '个性化设置',
					handler: function() {
						var app = HteOS.AppManager.getByKey("settings");
						if (app) {
							HteOS.TaskManager.start(app);
						} else {
							HteOS.Notification.show("", "应用提示",
									"运行[个性化设置]，请先安装[个性化设置]应用");
						}
					}
				},{
					id :'profile',
					icon : 'glyphicon glyphicon-user',
					name :'我的HteOS',
					handler :function(){
						var app = HteOS.AppManager.getByKey("myhteos");
						if (app) {
							HteOS.TaskManager.start(app);
						} else {
							HteOS.Notification.show("", "应用提示",
									"运行[个人中心]失败，请先安装[个人中心]应用");
						}
					}
				},'-',{
					id : 'about',
					name: '关于HteOS',
					icon: 'glyphicon glyphicon-info-sign',
					handler :function(){
						HteOS.Messager.alert("欢迎",HteOS.introduction);
					}
				}],
		
		tileContextMenu : [{
				id: 'open',
				icon: 'glyphicon glyphicon-new-window',
				name: '打开',
				handler: function() {
					HteOS.TaskManager.start(this.app);
				}
			},{
				id :'hide',
				icon : 'glyphicon glyphicon-eye-close',
				name :'隐藏',
				handler :function(){
					HteOS.AppManager.hide(this.app);
				}
			},{
				id :'size',
				icon : 'glyphicon glyphicon-transfer',
				name :'调整大小',
				items : [{
					id : 'large',
					name :'大',
					group : 'size',
					handler :function(){
						this.app.setSize("large");
					}
				},{
					id : 'default',
					name :'宽',
					group : 'size',
					handler :function(){
						this.app.setSize("default");
					}
				},{
					id : 'mini',
					name :'小',
					group : 'size',
					handler :function(){
						this.app.setSize("mini");
					}
				}]
			},{
				id :'addto',
				icon : 'glyphicon glyphicon-log-in',
				name :'添加到',
				items : [{
					id : 'quick',
					name : "快速启动栏",
					handler :function(){
						HteOS.AppManager.pink(this.app);
					}
				},{
					id : 'newgroup',
					name : "新建分组",
					handler : function(){
						HteOS.AppManager.addToNewGroup(this.app);
					}
				}]
			},'-',{
				id :'uninstall',
				icon : 'glyphicon glyphicon-trash',
				name :'卸载',
				handler :function(){
					HteOS.app.AppManager.uninstall(this.app);
				}
			}],
		
		init : function(){
			this.initTileMenu();
			this.initContextMenu();
			//this.initGroupMenu();
		},
		
		/*initGroupMenu : function(){
			new HteOS.menu.Menu({
				selector : ".hte-metro-group-nav-item",
				items : [{
					id : 'rename',
					icon : 'glyphicon glyphicon-edit',
					name: '重命名',
					handler : function(){
						var nav = $(this.target);
						var group = HteOS.app.AppManager.groups[nav.data("groupId")];
						HteOS.Messager.prompt("重命名分组","输入新的分组名称",[{
							name : 'name',
							value : group.name
						}],function(btn,values){
							if(btn == 'yes'){
								console.log(values);
							}
						});
					}
				}]
			});
		},*/
		
		/**
		 *初始化磁贴的右键菜单
		 */
		initTileMenu: function() {
			new HteOS.menu.Menu({
				selector: ".hte-tile",
				cls: "hte-tile-contextmenu",
				items:this.tileContextMenu,
				events: {
					show: function(target) {
						var me = this,
							tile;
						if ($(target).hasClass("hte-tile")) {
							tile = $(target);
						} else {
							tile = $(target).closest(".hte-tile");
						}
						var app = HteOS.app.AppManager.getByEl(tile);
						if (app) {
							tile.addClass("selected");
							if (!app.path) {
								me.disable("open");
								me.disable("quick");
							} else {
								me.enable("open");
								if (app.quick) {
									me.disable("quick");
								} else {
									me.enable("quick");
								}
							}
							if (app.native) {
								me.disable("uninstall");
							} else {
								me.enable("uninstall");
							}
							
							me.group("size").find(".glyphicon").removeClass("glyphicon-ok");
							me.setIcon(app.size,'glyphicon-ok');
							me.app = app;
						} else {
							me.hide();
						}
					},
					hide: function() {
						$(".hte-tile.selected").removeClass("selected");
						this.app = null;
					}
				}
			});
		},

		/**
		 * 初始化桌面右键菜单
		 * */
		initContextMenu: function() {
			var selector = (HteOS.UA.isIE8 || HteOS.UA.isIE9) ? ".hte-wallpaper-image" : ".hte-metro-container";
			new HteOS.menu.Menu({
				selector: selector,
				cls: "hte-context-menu",
				items: this.containerContextMenu,
				events: {
					show : function(){
						if(HteOS.User.logon === true){
							this.get("locker").show();
						}else{
							this.get("locker").hide();
						}
					}
				}
			});
		}
	};
}));